<template>
	<el-card>
		<div v-if="key == this.routePath.defaultPath">
			<!-- 搜索 -->
			<el-form :inline="true" :model="searchForm">
				<el-form-item label="使用状态">
					<el-select v-model="searchForm.available" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in availableOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="过期状态">
					<el-select v-model="searchForm.expired" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in expiredOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="优惠券模版名称">
					<el-input placeholder="优惠券模版名称" v-model="searchForm.templateName"></el-input>
				</el-form-item>
				<el-form-item label="优惠券分类">
					<el-select v-model="searchForm.templateCategory" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in templateCategoryOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
					<el-button class="filter-item" v-if="permission_add" type="primary" icon="el-icon-plus" @click="handleCreate">新增</el-button>
				</el-form-item>
			</el-form>
			<!-- 列表 -->
			<el-table :data="dataList" height="calc(100VH - 270px)" highlight-current-row border style="width: 100%" v-loading="loading">
				<el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
				<el-table-column label="模版名称" prop="templateName" align="center"></el-table-column>
				<el-table-column label="可用状态" prop="available" align="center" >
					<template slot-scope="scope">
						{{scope.row.available == '1' ? '可用':'不可用'}}
					</template>
				</el-table-column>
				<el-table-column label="过期状态" prop="expired" align="center" >
					<template slot-scope="scope">
						{{scope.row.expired == '1' ? '已过期':'未过期'}}
					</template>
				</el-table-column>
				<el-table-column label="优惠券分类" prop="templateCategory" align="center" >
					<template slot-scope="scope">
						<span v-if="(scope.row.templateCategory == '001')">满减券</span>
						<span v-if="(scope.row.templateCategory == '002')">折扣券</span>
						<span v-if="(scope.row.templateCategory == '003')">立减券</span>
					</template>
				</el-table-column>
				<el-table-column label="优惠券数量" prop="couponCount" align="center" ></el-table-column>
				<el-table-column label="创建时间" prop="createTime" align="center" width="160"></el-table-column>
				<template v-if="permission_receive">
					<el-table-column label="领取记录" align="center" width="100" fixed="right">
						<template slot-scope="scope">
							<el-button type="text" size="small" @click="handleViewReceive(scope.row.templateCode)">领取记录</el-button>
						</template>
					</el-table-column>
				</template>
				<el-table-column label="操作" align="center" width="150" fixed="right">
					<template slot-scope="scope">
						<el-button v-if="permission_view" type="text" size="small" @click="handleView(scope.row.id)">查看</el-button>
						<el-button v-if="permission_edit" type="text" size="small" @click="handleEdit(scope.row.templateCode)">修改</el-button>
						<el-button v-if="permission_delete" type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div v-show="!loading" class="pagination-container">
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageForm.pageNum"
				:page-sizes="pageForm.pageSizeSelect" :page-size="pageForm.pageSize" :total="pageForm.totalData" layout="total, sizes, prev, pager, next, jumper"
				:prev-text="pageForm.prevText" :next-text="pageForm.nextText">
				</el-pagination>
			</div>
		</div>
		<div v-else>
			<router-view :key="key"></router-view>
		</div>
	</el-card>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { findCouponTemplateList,deleteCouponTemplate } from '@/api/activity/couponTemplate'
	export default {
		name: 'couponTemplate',
		data() {
			return {
				// 默认加载弹出层
				loading: true,
				//路由地址
				routePath: {
					defaultPath: '/activity/coupon',
					addPath: '/activity/coupon/add',
					editPath: '/activity/coupon/edit',
					viewPath: '/activity/coupon/view',
					viewReceivePath: '/activity/coupon/receive'
				},
				// 数据列表el-table
				dataList: [],
				// 权限按钮控制
				permission_add: false,
				permission_edit: false,
				permission_delete: false,
				permission_view: false,
				permission_receive: false,
				// 搜索条件
				searchForm: {
					id: '',
					available: '',
					expired: '',
					templateName: '',
					templateLogo: '',
					templateIntro: '',
					templateCategory: '',
					couponCount: '',
					createTime: '',
					userCode: '',
					templateKey: '',
					target: '',
					rule: '',
					productLine: '',
					templateCode: '',
					pageNum: 1,
					pageSize: 10
				},
				//分页相关
				pageForm: {
					pageSizeSelect: [10, 20, 30, 50], //每页显示个数选择器的选项设置
					totalData: 0, //总条数
					pageSize: 10, //每页显示条目个数
					pageNum: 1, //当前页
					prevText: '上一页', //替代图标显示的上一页文字
					nextText: '下一页' //替代图标显示的下一页文字
				},
				availableOptions: [
					{
						label: '可用',
						value: 1
					},
					{
						label: '不可用',
						value: 2
					},
				],
				expiredOptions: [
					{
						label: '已过期',
						value: 1
					},
					{
						label: '未过期',
						value: 2
					},
				],
				templateCategoryOptions: [
					{
						label: '满减券',
						value: '001'
					},
					{
						label: '折扣券',
						value: '002'
					},
					{
						label: '立减券',
						value: '003'
					},
				],
				
				
			}
		},
		computed: {
			key() {
				return this.$route.path
			},
			...mapGetters([
				'elementList'
			])
		},
		components: {},
		created() {
			if (this.$route.path === this.routePath.defaultPath) {
				// 初始化获取数据列表
				this.getDateList()
				//初始化按钮权限
				this.permission_add = this.elementList['activity:coupon:add']
				this.permission_edit = this.elementList['activity:coupon:edit']
				this.permission_delete = this.elementList['activity:coupon:delete']
				this.permission_view = this.elementList['activity:coupon:detail']
				this.permission_receive = this.elementList['activity:coupon:receive']
			}
		},
		methods: {
			// 方法集合
			getDateList() {
				this.loading = true
				findCouponTemplateList(this.searchForm).then(res => {
					this.dataList = res.data.responseData
					this.pageForm.totalData = parseInt(res.data.total)
				})
				this.loading = false
			},
			handleView(val) {
				this.$router.push({path: this.routePath.viewPath,query: {id: val}});
			},
			handleEdit(val) {
				this.$router.push({path: this.routePath.editPath,query: {templateCode: val}});
			},
			handleDelete(val) {
				this.$confirm('是否要删除?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deleteCouponTemplate(val).then(() => {
						this.$notify({
							title: '成功',
							message: '删除成功',
							type: 'success',
							duration: 2000,
							onClose: this.getDateList()
						});
					}).catch((err) => {
						this.$message.error(err.data.responseMessage);
					});
				});
			},
			handleCreate() {
				this.$router.push({path: this.routePath.addPath});
			},
			handleSearch() {
				this.getDateList();
			},
			handleSizeChange(val) {
				this.searchForm.pageSize = val
				this.getDateList()
			},
			handleCurrentChange(val) {
				this.searchForm.pageNum = val
				this.getDateList()
			},
			handleViewReceive(val){
				this.$router.push({path: this.routePath.viewReceivePath,query: {templateCode: val}});
			}

		}
	}
</script>
